package com.zhkj.txg.module.login.ui

import androidx.lifecycle.Observer
import com.zhkj.lib.base.ViewModelFragment
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.txg.R
import com.zhkj.txg.module.login.vm.LoginViewModel
import com.zhkj.lib.utils.AccountManager
import kotlinx.android.synthetic.main.fragment_invite.*

class InviteFragment : ViewModelFragment<LoginViewModel>() {

    override fun setViewModel(): LoginViewModel = createViewModel(true)

    override val layoutResId: Int = R.layout.fragment_invite

    override fun initUI() {
        btnConfirm.setOnClickListener {
            val token = AccountManager.getToken()
            viewModel.bindInvite(token, etInviteCode.text.toString()).takeIf {
                !it.hasObservers()
            }?.observe(viewLifecycleOwner, Observer {
                handlerResponseStatus(it.status, it.statusTip)
                if (it.status == HttpResponseStatus.Status.SUCCESS) {
                    viewModel.pageIndexLiveData.value = LoginActivity.Index.REGISTER
                }
            })
        }
        btnSkip.setOnClickListener {
            viewModel.pageIndexLiveData.value = LoginActivity.Index.REGISTER
        }
    }
}